<?php
/**
 * Created by PhpStorm.
 * User: stupboy
 * Date: 2018/6/22
 * Time: 15:08
 * Mail:87658989@qq.com
 */

namespace app\adminapi\logic;

use think\Db;
use think\Config;

class Unit
{
    /**
     * @param int $type
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function orderTypeCount( $type = 1 ){
        $field  = $type == 1 ? 'use_type,count(order_id) value' : 'use_explain name,count(order_id) value';
        $group  = $type == 1 ? 'use_type' : 'use_explain';
        $orders = Db::name('order')
            ->field($field)
            ->where('order_type',1)
            ->where('use_type','gt',0)
            ->where('order_state','in','9,10,11')
            ->group($group)
            ->select();
        $temp = [];
        $types= Config::get('base.use_types');
        $title= [];
        foreach ($orders as $order){
            if($type==1){
                $title[] = $types[$order['use_type']];
                $order['name'] = $types[$order['use_type']];
                unset($order['use_type']);
            }else{
                $title[] = $order['name'];
            }
            $temp[] = $order;
        }
        $data['data']  = $temp;
        $data['title'] = $title;
        return $data;
    }
}